package okhttp3;

import java.io.Closeable;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
import o.ftz;
import o.fua;
import o.fum;
import o.fus;
import o.fuv;
import o.fva;
import o.fvb;
import o.fve;
import o.fvg;
import o.fvl;
import o.fvp;
import o.fvq;
import o.fvu;
import o.fvw;
import o.fvz;
import o.fwy;
import o.fxd;
import o.fxv;
import okhttp3.internal.connection.StreamAllocation;

public final class RealCall implements ftz {
    final fuv client;
    @Nullable
    private fum eventListener;
    private boolean executed;
    public final boolean forWebSocket;
    final fus originalRequest;
    final fvw retryAndFollowUpInterceptor;
    final fxd timeout = new fxd(this) {
        final /* synthetic */ RealCall ˋ;

        {
            this.ˋ = r1;
        }

        public void ॱ() {
            this.ˋ.cancel();
        }
    };

    public final class d extends fva {
        static final /* synthetic */ boolean ˊ = (!RealCall.class.desiredAssertionStatus());
        private boolean ˋ = false;
        private final fua ˎ;
        final /* synthetic */ RealCall ˏ;

        public d(RealCall realCall, fua o_fua) {
            this.ˏ = realCall;
            super("OkHttp %s", realCall.redactedUrl());
            this.ˎ = o_fua;
        }

        public String ˊ() {
            return this.ˏ.originalRequest.ˏ().ʻ();
        }

        public fus ˎ() {
            return this.ˏ.originalRequest;
        }

        public RealCall ॱ() {
            return this.ˏ;
        }

        public void ॱ(ExecutorService executorService) {
            if (ˊ || !Thread.holdsLock(this.ˏ.client.ˈ())) {
                try {
                    executorService.execute(this);
                } catch (Throwable e) {
                    IOException interruptedIOException = new InterruptedIOException("executor rejected");
                    interruptedIOException.initCause(e);
                    this.ˏ.eventListener.callFailed(this.ˏ, interruptedIOException);
                    this.ˎ.onFailure(this.ˏ, interruptedIOException);
                } finally {
                    this.ˏ.client.ˈ().ˎ(this);
                }
            } else {
                throw new AssertionError();
            }
        }

        public fuv ˏ() {
            return this.ˏ.client;
        }

        public boolean ˋ() {
            return this.ˋ;
        }

        public void ʼ() {
            this.ˋ = true;
        }

        public void ॱॱ() {
            Throwable timeoutExit;
            this.ˏ.timeout.ar_();
            try {
                fvb responseForCreateConnectionOnly;
                if (this.ˋ) {
                    responseForCreateConnectionOnly = this.ˏ.getResponseForCreateConnectionOnly();
                } else {
                    responseForCreateConnectionOnly = this.ˏ.getResponseWithInterceptorChain();
                }
                this.ˎ.onResponse(this.ˏ, responseForCreateConnectionOnly);
                this.ˏ.client.ˈ().ˎ(this);
            } catch (IOException e) {
                timeoutExit = this.ˏ.timeoutExit(e);
                if (null != null) {
                    fwy.ˋ().ˏ(4, "Callback failure for " + this.ˏ.toLoggableString(), timeoutExit);
                } else {
                    this.ˏ.eventListener.callFailed(this.ˏ, timeoutExit);
                    this.ˎ.onFailure(this.ˏ, timeoutExit);
                }
                this.ˏ.client.ˈ().ˎ(this);
            } catch (Throwable timeoutExit2) {
                if (null != null) {
                    fwy.ˋ().ˏ(4, "Callback failure for " + this.ˏ.toLoggableString(), timeoutExit2);
                } else {
                    this.ˏ.eventListener.callFailed(this.ˏ, new IOException(timeoutExit2));
                    this.ˎ.onFailure(this.ˏ, new IOException(timeoutExit2));
                }
                this.ˏ.client.ˈ().ˎ(this);
            } catch (Throwable th) {
                this.ˏ.client.ˈ().ˎ(this);
            }
        }
    }

    private RealCall(fuv o_fuv, fus o_fus, boolean z) {
        this.client = o_fuv;
        this.originalRequest = o_fus;
        this.forWebSocket = z;
        this.retryAndFollowUpInterceptor = new fvw(o_fuv, z);
        this.timeout.ˊ((long) o_fuv.ˎ(), TimeUnit.MILLISECONDS);
    }

    public static RealCall newRealCall(fuv o_fuv, fus o_fus, boolean z) {
        RealCall realCall = new RealCall(o_fuv, o_fus, z);
        realCall.eventListener = o_fuv.ˋˊ().create(realCall);
        return realCall;
    }

    public fus request() {
        return this.originalRequest;
    }

    public fvb execute() throws IOException {
        synchronized (this) {
            if (this.executed) {
                throw new IllegalStateException("Already Executed");
            }
            this.executed = true;
        }
        captureCallStackTrace();
        this.timeout.ar_();
        this.eventListener.callStart(this);
        try {
            this.client.ˈ().ˊ(this);
            fvb responseWithInterceptorChain = getResponseWithInterceptorChain();
            if (responseWithInterceptorChain == null) {
                throw new IOException("Canceled");
            }
            this.client.ˈ().ˎ(this);
            return responseWithInterceptorChain;
        } catch (IOException iOException) {
            IOException iOException2 = timeoutExit(iOException2);
            this.eventListener.callFailed(this, iOException2);
            throw iOException2;
        } catch (Throwable e) {
            this.eventListener.callFailed(this, new IOException(e));
            throw new IOException(e);
        } catch (Throwable th) {
            this.client.ˈ().ˎ(this);
        }
    }

    @Nullable
    public IOException timeoutExit(@Nullable IOException iOException) {
        if (!this.timeout.ˋ()) {
            return iOException;
        }
        IOException interruptedIOException = new InterruptedIOException("timeout");
        if (iOException != null) {
            interruptedIOException.initCause(iOException);
        }
        return interruptedIOException;
    }

    private void captureCallStackTrace() {
        this.retryAndFollowUpInterceptor.ˋ(fwy.ˋ().ˏ("response.body().close()"));
    }

    public void enqueue(fua o_fua) {
        synchronized (this) {
            if (this.executed) {
                throw new IllegalStateException("Already Executed");
            }
            this.executed = true;
        }
        captureCallStackTrace();
        this.eventListener.callStart(this);
        this.client.ˈ().ॱ(new d(this, o_fua));
    }

    public void cancel() {
        this.retryAndFollowUpInterceptor.ˊ();
    }

    public fxv timeout() {
        return this.timeout;
    }

    public synchronized boolean isExecuted() {
        return this.executed;
    }

    public boolean isCanceled() {
        return this.retryAndFollowUpInterceptor.ˎ();
    }

    public RealCall clone() {
        return newRealCall(this.client, this.originalRequest, this.forWebSocket);
    }

    StreamAllocation streamAllocation() {
        return this.retryAndFollowUpInterceptor.ॱ();
    }

    String toLoggableString() {
        return (isCanceled() ? "canceled " : "") + (this.forWebSocket ? "web socket" : "call") + " to " + redactedUrl();
    }

    String redactedUrl() {
        return this.originalRequest.ˏ().ͺ();
    }

    fvb getResponseWithInterceptorChain() throws IOException {
        List arrayList = new ArrayList();
        arrayList.addAll(this.client.ʾ());
        arrayList.add(this.retryAndFollowUpInterceptor);
        arrayList.add(new fvq(this.client.ॱॱ()));
        arrayList.add(new fve(this.client.ʻ()));
        arrayList.add(new fvl(this.client));
        if (!this.forWebSocket) {
            arrayList.addAll(this.client.ˊᐝ());
        }
        arrayList.add(new fvu(this.forWebSocket));
        Closeable ॱ = new fvz(arrayList, null, null, null, 0, this.originalRequest, this, this.eventListener, this.client.ˋ(), this.client.ˏ(), this.client.ॱ()).ॱ(this.originalRequest);
        if (!this.retryAndFollowUpInterceptor.ˎ()) {
            return ॱ;
        }
        fvg.ॱ(ॱ);
        throw new IOException("Canceled");
    }

    fvb getResponseForCreateConnectionOnly() throws IOException {
        List arrayList = new ArrayList();
        arrayList.add(this.retryAndFollowUpInterceptor);
        arrayList.add(new fvp(this.client));
        return new fvz(arrayList, null, null, null, 0, this.originalRequest, this, this.eventListener, this.client.ˋ(), this.client.ˏ(), this.client.ॱ()).ॱ(this.originalRequest);
    }
}
